package com.uottawa.library.service;

import java.util.List;

import com.uottawa.library.dao.LoanDAO;
import com.uottawa.library.dao.TitleDAO;
import com.uottawa.library.domain.Loan;
import com.uottawa.library.domain.Title;

public class TitleService {
	
	private TitleDAO dao = new TitleDAO();
	private LoanDAO loanDao = new LoanDAO();
	
	public String addTitle(int isbn, String name) {
		Title title = new Title(isbn);
		title.setName(name);
		boolean added = dao.addTitle(title);
		String result = (added) ? "success" : "failed! duplicate title";
		return result;
	}

	public String removeTitle(int id) {
		List<Loan> loans = loanDao.getActiveLoansByTitleIsbn(id);
		if(!loans.isEmpty()) {
			return "rejected! can't remove a title that is currently loaned";
		}
		boolean removed = dao.RemoveTitle(id);
		String result = (removed) ? "success" : "failed";
		return result;
	}

	public List<Title> getAllTitles() {
		return dao.getAllTitles();
	}

	public String updateTitle(int isbn, String name) {
		boolean updated = dao.updateTitle(isbn, name);
		String result = (updated) ? "success" : "failed";
		return result;
	}
}
